/*----------------------------------------------------------------------------
 | Copyright (c) 2009 Matthew B. Brophy (matt@brophy.org).  
 | Liscense: MIT License (http://www.opensource.org/licenses/mit-license.php)
 |
 | $Id: select.js 18 2009-03-13 14:32:58Z matt@brophy.org $
 -----------------------------------------------------------------------------*/

if (typeof org == "undefined" || 
    org == null) { org = {}; }
if (typeof org.brophy == "undefined" || 
    org.brophy == null) { org.brophy = {}; }
if (typeof org.brophy.js == "undefined" || 
    org.brophy.js == null) { org.brophy.js = {}; }

/** 
 * @namespace 
 * @description Library of useful functions for dealing with SELECT elements
 *              in HTML pages.
 * @requires    This library requires the <a href="http://www.mochikit.com">
 *              MochiKit</a> JavaScript library 
 *              (version 1.4+).
 */
org.brophy.js.select = {};

/**
 * @description  Selects the OPTION element with value 'val' in the SELECT 
 *               element 'sel'
 * @param        sel
 *               The SELECT element
 * @param        [val] 
 *               The value of the OPTION element to set as selected.  If no 
 *               value is passed, then the first item in the select box will 
 *               be selected
 * @return       TRUE if the value is found and the OPTIONselected, FALSE 
 *               otherwise
 * @type         boolean
 */
org.brophy.js.select.setSelectedIndexByValue = function(sel, val)
{
    if( MochiKit.Base.isUndefinedOrNull(sel) || 
        MochiKit.Base.isUndefinedOrNull(sel.options) ) { return false; }
    
    if(MochiKit.Base.isUndefinedOrNull(val))
    {
      return org.brophy.js.select.setSelectedIndex(sel); 
    }
    else
    {
      return org.brophy.js.select.setSelectedIndex(
               sel,
               org.brophy.js.array.indexOf(
                 sel.options,
                 val,
                 function(a,b) {return MochiKit.Base.compare(a.value,b);}));
    }    
}

/**
 * @description  Selects the OPTION element with index 'index' in the SELECT 
 *               element 'sel'
 * @param        sel
 *               The SELECT element
 * @param        [index = 0] 
 *               The integer index of the OPTION element to select.  If no 
 *               index is passed, then the first item in the select box will be
 *               selected
 * @return       TRUE if the OPTION is present and selected, FALSE otherwise
 * @type         boolean
 */
org.brophy.js.select.setSelectedIndex = function(sel, index)
{
  if( MochiKit.Base.isUndefinedOrNull(sel) ||
      MochiKit.Base.isUndefinedOrNull(sel.options) )
    { return false; }
 
  if(MochiKit.Base.isUndefinedOrNull(index)) { index = 0; }
     
  for(var i = 0; i < sel.options.length; i++)
    { sel.options[i].selected = (i == index); }
  
  sel.selectedIndex = index;
  return (sel.selectedIndex == index);
}

/**
 * @description  Sorts the OPTIONS in the SELECT element 'sel' based on the 
 *               display value of the OPTION.  IF neccesary, the ignore_n 
 *               parameter can specify the number of options to ignore, in case
 *               there are default or information values first in the list.
 * @param        sel
 *               The SELECT element
 * @param        [ignore_n = 0] 
 *               The number of OPTION elements to ignore at the beginning of 
 *               the SELECT box.  If this is not passed, no elements will be 
 *               ignored.
 * @return       TRUE if the SELECT element is sorted, FALSE otherwise.
 * @type         boolean
 */
org.brophy.js.select.sortSelectOptions = function(sel, ignore_n)
{
  if(MochiKit.Base.isUndefinedOrNull(sel)) { return null; }
  var obj = org.brophy.js;
  var localOpts = [];
  while(sel.options.length > 0)
    { localOpts.push(MochiKit.DOM.removeElement(sel.options[0])); }

  localOpts = obj.array.sortArrayOfObjects(localOpts, "text", null, ignore_n);

  MochiKit.Iter.forEach(localOpts, function(opt)
    { 
      MochiKit.DOM.appendChildNodes(sel, opt);
    });

  org.brophy.js.select.setSelectedIndex(sel);
  return true;
}

/**
 * @description  Function to gray out a select box when the first option 
 *               is selected, under the assumption that this is a default, 
 *               informational option.  Please note that in order for this 
 *               to work correctly, the select must be given a CSS class of 
 *               'obj_DefaultSelect'
 * 
 * @param        e
 *               The event first from the &lt;select&gt; onchange event
 */
org.brophy.js.select.setDefaultSelectStyle = function(e)
{
  sel = org.brophy.js.event.getElement(e);
  if(MochiKit.Base.isUndefinedOrNull(sel)) { return null; }
  if(sel.selectedIndex === 0)
  {
    MochiKit.DOM.addElementClass(sel,"obj_gray");
    MochiKit.DOM.removeElementClass(sel,"obj_black");
  }
  else
  {
    MochiKit.DOM.addElementClass(sel,"obj_black");
    MochiKit.DOM.removeElementClass(sel,"obj_gray");
  }
}

